package com.jy.wms.controller;


import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsrRailwayPlatformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsrRailwayPlatformController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author ln
 * @Date Tue Oct 10 10:08:00 CST 2017
 */
@Controller
@RequestMapping("/wmsrrailwayplatform")
@Api(value = "/wmsrrailwayplatform", description = "月台")
public class WmsrRailwayPlatformController extends BaseController<WmsrRailwayPlatformService,WmsrRailwayPlatform> {

    @ApiOperation(value = "月台 POST请求查询",response = JSONObject.class, notes = "POST请求查询月台")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject getWmsrRailwayPlatform(@RequestBody WmsrRailwayPlatform entity, HttpSession session) {
        Page<WmsrRailwayPlatform> list=new Page<WmsrRailwayPlatform>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
                list= baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            //json = SetStatus.setFunctionType(1,"error","月台",e.getMessage(),"");
        }
        return json;
    }


    @ApiOperation(value = "添加月台",response = JSONObject.class, notes = "POST新增月台")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject addWmsrRailwayPlatform(@RequestBody WmsrRailwayPlatform entity,HttpSession session) throws Exception {
        JSONObject json=new JSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setSshzid(hz.get(0).getHzid());
            baseService.baseInsert(entity);
         }catch (SucessException e){
            json=SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增月台成功","", JSONObject.fromObject(entity).toString());
        }
        catch (ServiceException e){
            e.printStackTrace();
            json=SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增月台失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增月台失败",e.getMessage(),"");
        }
        return json;
    }

    @ApiOperation(value = "库位组 POST请求查询",response = JSONObject.class, notes = "POST请求查询库位组")
    @PostMapping(value = "/kwzSelectPage")
    public  @ResponseBody
    JSONObject kwzSelectPage(@RequestBody WmsLibrarypositiongroup entity,HttpSession session) {
        Page<WmsLibrarypositiongroup> list=new Page<WmsLibrarypositiongroup>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            entity.setKwzlx(PlatformInfo.baseGroup_kwzlx.getType());
            entity.setSfyx(PlatformInfo.baseGroup_sfyx.getType());
            list= baseService.kwzSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "删除月台",response = JSONObject.class, notes = "POST删除月台")
    @PostMapping(value = "/baseDeleteBathPrimaryKeys")
    public  @ResponseBody JSONObject deleteWmsrRailwayPlatform(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) throws Exception {
        JSONObject json=new JSONObject();
        try {
            baseService.baseDeleteBathPrimaryKeys(setModel.getIds());
        }catch (SucessException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除月台成功","","");
        }catch (ServiceException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(), "删除月台失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(), "删除月台失败",e.getMessage(),"");
        }
        return json;
    }

    @GetMapping(value = "/baseSelectByPrimaryKey/{id}")
    @ApiOperation(value = "根据id查询月台",response = JSONObject.class, notes = "GET查看单个月台")
    public  @ResponseBody JSONObject baseSelectByPrimaryKey(@PathVariable("id") String id) {
        JSONObject json = new JSONObject();
        WmsrRailwayPlatform entity= null;
        try {
            entity = baseService.baseSelectByPrimaryKey(id);
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(), "查询月台失败","","");
        }
        return json;
    }

    @ApiOperation(value = "编辑月台",response = JSONObject.class, notes = "POST编辑月台")
    @PostMapping(value = "/baseUpdateByPrimaryKeySelective")
    public  @ResponseBody JSONObject baseUpdateByPrimaryKeySelective(@RequestBody WmsrRailwayPlatform entity) throws Exception {
        JSONObject json=new JSONObject();
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
        }catch (SucessException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"编辑月台成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑月台失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json=SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑月台失败",e.getMessage(),"");
        }
        return json;
    }

    /**
     * 模糊查询货主信息
     * @param map
     * @return
     */
    /*@PostMapping(value = "/hzSearch",produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "月台 货主模糊查询",response = JSONObject.class, notes = "查询输入{\"mc\":\"货主信息模糊查询\"}")
    public @ResponseBody
    JSONArray hzSearch(@RequestBody Map<String,String> map) {
        JSONArray jsonArray = new JSONArray();
        try {
            List<WmsOwner> list = baseService.hzSearch(map.get("mc"));
            for (int i = 0; i < list.size(); i++) {
                WmsOwner WmsOwner =  list.get(i);
                JSONObject json = new JSONObject();
                json.put("dictionaryid",WmsOwner.getId());
                json.put("value",WmsOwner.getMc());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return jsonArray;
    }*/
}
